<?php
/**
 * 同步小区详情
 */

namespace app\crontab\controller\once;


use app\crontab\model\HouseVillage;
use custom\Log;
use custom\Mongo;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Exception;

class HouseVillageInfo extends Command {

    static $dbname = 'test';
    static $set = 'ajk_village_info_guangzhou';


    protected function configure() {
        $this->setName('HouseVillageInfo');
    }

    protected function execute(Input $input, Output $output) {
        $this->initial();
    }

    protected function initial() {
        echo "runing\n";
        $mongo = Mongo::getInstance();
        try {
            $long = 100;//一页处理多少条
            $where = [];//条件
            $time1 = get_microtime();
//            $where=['years'=>['$eq' => '2015年']];
            $model = new HouseVillage();
            $err = [];
            $count = $mongo->getCount(self::$set, $where);//总条数
            $pageNum = ceil($count / $long);//总页数
            for ($page = 1; $page <= $pageNum; $page++) {
                $cursor = $mongo->page($where, $page, $long, self::$set, $count);//分页
                $time2 = get_microtime();
                $list = $cursor['data'];
                foreach ($list as $k => $v) {
                    $data = (array)$v;
                    $_id = ((array)$data['_id'])['oid'];
                    $findOne = $model->where('_id', $_id)->field('id')->find()->toArray();
                    $update = [
                        'id'             => $findOne['id'],
                        'property'       => $data['property_type'] ?? '',//物业类型
                        'cost'           => $data['property_fee'] ?? '',//物业费
                        'acreage'        => $data['build_area'] ?? '',//总建面积
                        'person_num'     => $data['total_household'] ?? '',//总户数
                        'years'          => $data['years'] ?? '',//建造年代
                        'parking_space'  => $data['parking_space'] ?? '',//停车位
                        'plot_ratio'     => $data['plot_ratio'] ?? '',//容积率
                        'greening_rate'  => $data['greening_rate'] ?? '',//绿化率
                        'developers'     => $data['developers'] ?? '',//开发商
                        'tenement'       => $data['property_company'] ?? '',//物业公司
                        'related_school' => $data['related_schools'] ?? '',//相关学校
                        'cover'          => $data['cover'] ?? '',//封面
                        'source_url'     => $data['source_url'] ?? '',//小区url
                        'address'        => $data['address'] ?? '',//商圈地址
                    ];
                    if (!($model->where('id', $findOne['id'])->update($update))) {
                        $err[] = $findOne['id'];
                    }
//                    $time3 =  get_microtime();
//                    $diff1 = $time2 - $time1;
//                    $diff3 = $time3 - $time1;
//                    echo "diff1:{$diff1}||diff3:{$diff3}";
//                    exit;
                }
            }
            echo "end\n";
            Log::write($err, "HouseVillageInfo_ErrData.log");
        } catch (Exception $e) {
            $err = $e->getMessage() . ' (' . $e->getCode() . '), ' . $e->getFile() . ' (' . $e->getLine() . ')' . "\n" . $e->getTraceAsString() . "\r\n\r\n";
            Log::write($err, "HouseVillageInfoErr.log");
        }
    }

}